<template>
<div class="box searchResBox">
  <div class="searchWrap">
    <van-icon name="arrow-left" @click="goBack"/>
    <van-search
      v-model="s"
      shape="round"
      background="#fff"
    />
    <van-icon name="shopping-cart-o" @click="$router.push('/cart')"/>
  </div>
  <div class="sort">
    <ul>
      <li v-for="item in options" :key="item.value" @click="onSearch(item)" class="sortLi">{{item.text}}</li>
    </ul>
  </div>
    <div class="main">
      <div class="pro" v-for="(item,idx) in renderList" :key="idx" @click="$router.push('/detail/'+item.proId)">
        <img :src="item.img">
        <h2 class="title">
          {{item.proname}}
        </h2>
        <p class="sTitle">{{item.desc}}</p>
        <div class="Bstock">
          <span class="stock">
          库存：{{item.stock}}
          </span>
        </div>
        <div class="Bstock">
          <span class="stock">
          销量：{{item.sales}}
          </span>
        </div>
        <div class="price">
          <p class="num">
            ￥{{item.lastPri}}
          </p>
          <p class="add" @click.stop="addCart(item.proId)">+</p>
        </div>
    </div>
  </div>
</div>
</template>
<script>
import Vue from 'vue'
import { Icon, Search, Toast } from 'vant'
import { getSearch, addCart } from '@/api/request.js'

Vue.use(Icon)
Vue.use(Search)
Vue.use(Toast)
export default {
  props: ['s'],
  data () {
    return {
      options: [
        { text: '默认排序', value: 0 },
        { text: '价格升序', value: 1 },
        { text: '价格降序', value: 2 },
        { text: '销量排序', value: 3 }
      ],
      searchList: [],
      renderList: [],
      sortLi: document.getElementsByClassName('sortLi')
    }
  },
  methods: {
    goBack () {
      this.$router.back()
    },
    addCart (id) {
      addCart({
        userId: sessionStorage.getItem('userId'),
        proId: id,
        num: 1
      }).then(data => {
        if (data.data.code !== 202 && sessionStorage.getItem('isLogin') === 'ok') {
          Toast('添加购物车成功')
        } else {
          Toast('添加购物车失败')
        }
      })
    },
    onSearch (item) {
      for (var i = 0; i < this.sortLi.length; i++) {
        this.sortLi[i].style.background = '#eee'
      }
      this.sortLi[item.value].style.background = '#ff258d'
      switch (item.value) {
        case 0:
          this.renderList = [...this.searchList]
          break
        case 1:
          this.renderList = [...this.searchList]
          this.renderList.sort((a, b) => {
            return a.lastPri - b.lastPri
          })
          break
        case 2:
          this.renderList = [...this.searchList]
          this.renderList.sort((a, b) => {
            return b.lastPri - a.lastPri
          })
          break
        case 3:
          this.renderList = [...this.searchList]
          this.renderList.sort((a, b) => {
            return b.sales - a.sales
          })
          break
      }
    }
  },
  mounted () {
    getSearch({
      sKey: this.s
    }).then(data => {
      console.log(data.data.msg)
      this.searchList = data.data.msg
      this.renderList = [...this.searchList]
    })
  }
}
</script>
<style lang="scss">
.main{
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  background: #f8f8f8;
  flex: 1;
  overflow: auto;
  .pro{
    width: 42vw;
    overflow: hidden;
    margin-bottom: 0.2rem;
    background: white;
    border-radius: 0.1rem;
    img{
      width: 1.6rem;
    }
    .title{
      font-size: 0.2rem;
      white-space: nowrap;
      text-overflow:ellipsis;
      overflow: hidden;
    }
    .sTitle{
      font-size: 0.1rem;
      color: rgb(126, 126, 126);
      white-space: nowrap;
      text-overflow:ellipsis;
      overflow: hidden;
    }
    .Bstock{
      padding: 0.05rem 0;
      .stock{
        background: rgb(255, 227, 232);
        border-radius: 0.12rem;
        font-size: 0.12rem;
        padding: 0 0.05rem;
        color:red
      }
    }
    .price{
      display: flex;
      justify-content: space-between;
      align-items: center;
      .add{
        padding:0 0.07rem;
        background: #ff258d;
        border-radius: 50%;
        color: white;
        font-size: 0.3rem;
        box-sizing: border-box;
        margin-top: -0.1rem;
      }
    }
  }
}
.sort{
  padding: 0.05rem 0.05rem;
  height: 0.25rem;
  ul{
    height: 100%;
    display: flex;
    li{
      height: 100%;
      padding: 0 0.05rem;
      line-height: 0.25rem;
      background: #eee;
      box-sizing: border-box;
      font-size: 0.12rem;
      border-radius: 0.12rem;
      text-align: center;
      margin-right: 0.15rem;
    }
  }
}
.van-icon-shopping-cart-o{
  font-size: 0.3rem;
}
.searchResBox{
  display: flex;
  flex-direction: column;
  background: white;
  padding: 0 0.21rem;
  .searchWrap{
    width: 100%;
    height: 0.74rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .van-icon-arrow-left{
      font-size: 0.25rem;
      color: #000;
    }
    .van-search{
      padding: 0 0.15rem;
      flex: 1;
      .van-search__content--round{
        .van-icon-search{
          font-size: 0.21rem;
          line-height: 0.35rem;
        }
        .van-field__control{
          font-size: 0.2rem;
          line-height: 0.3rem;
        }
      }
    }
  }
}
</style>
